﻿<ResourceDictionary 
      xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
      xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
      xmlns:sl="clr-namespace:SLPropertyGrid;assembly=SLPropertyGrid" 
      xmlns:basics="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls.Toolkit"
      >

    <Style TargetType="sl:PropertyGrid">
        <Setter Property="Template">
            <Setter.Value>

                <!-- ControlTemplate -->
                <ControlTemplate TargetType="sl:PropertyGrid">
                    <ScrollViewer VerticalScrollBarVisibility="Auto" HorizontalScrollBarVisibility="Disabled">
                        <ScrollViewer.Resources>

                        </ScrollViewer.Resources>
                        <StackPanel>
                            <Grid>
                                <Grid.ColumnDefinitions>
                                    <ColumnDefinition x:Name="def"  />
                                    <ColumnDefinition Width="Auto" />
                                    <ColumnDefinition Width="Auto"/>
                                </Grid.ColumnDefinitions>

                                <Grid  />
                                <!--

								<basics:GridSplitter x:Name="grsplSplitter"   Grid.Column="1" HorizontalAlignment="Center" 
								VerticalAlignment="Stretch" Background="Aqua" Width="5" 
								></basics:GridSplitter>-->
                            </Grid>
                            <ItemsControl ItemsSource="{TemplateBinding Properties}" Margin="0" Padding="0">
                                <ItemsControl.ItemTemplate>
                                    <DataTemplate>

                                        <!--<controls:Expander IsExpanded="True"  FontFamily="Verdana" FontSize="9">
                                            <controls:HeaderedContentControl.Header>
                                                <TextBlock Text="{Binding Name}" 
                                                           Padding="0" FontFamily="Verdana" FontSize="9"/>
                                            </controls:HeaderedContentControl.Header>
                                            <controls:HeaderedContentControl.Content>-->

                                        <ItemsControl ItemsSource="{Binding Properties}" >

                                            <ItemsControl.Resources>
                                                <sl:PropertiesTemplateSelector x:Key="propertiesTemplateSelector" />
                                                <sl:EnumTypeConverter x:Key="enumTypeConverter" />
                                                <sl:EnumValueConverter x:Key="enumValueConverter" />

                                                <DataTemplate x:Key="Type:ValueType">
                                                    <Grid >
                                                        <Grid.ColumnDefinitions>
                                                            <ColumnDefinition Width="40*" />
                                                            <ColumnDefinition Width="60*" />
                                                        </Grid.ColumnDefinitions>
                                                        <TextBlock  Margin="0,0,1,0" TextAlignment="Right" 
                                                                            Padding="0" FontFamily="Verdana" FontSize="9"
											 Text="{Binding Name}" VerticalAlignment="Top" Grid.Column="0" />
                                                        <TextBox Text="{Binding Value, Mode=TwoWay, NotifyOnValidationError=true,ValidatesOnExceptions=true}"  
															HorizontalAlignment="Stretch" Width="{TemplateBinding Width}" x:Name="default"  AcceptsReturn="True" 
                                                                         Margin="0" Padding="2,0,2,0" FontFamily="Verdana" FontSize="9" BorderThickness="1"
															IsEnabled="{Binding CanWrite}" Grid.Column="1"
															>
                                                        </TextBox>
                                                    </Grid>

                                                </DataTemplate>
                                                <DataTemplate x:Key="Type:String">
                                                    <Grid >
                                                        <Grid.ColumnDefinitions>
                                                            <ColumnDefinition Width="40*" />
                                                            <ColumnDefinition Width="60*" />
                                                        </Grid.ColumnDefinitions>
                                                        <TextBlock  Margin="0,0,1,0" TextAlignment="Right" 
                                                                            Padding="0" FontFamily="Verdana" FontSize="9"
											 Text="{Binding Name}" VerticalAlignment="Top" Grid.Column="0" />

                                                        <TextBox Text="{Binding Value, Mode=TwoWay, NotifyOnValidationError=true,ValidatesOnExceptions=true}"  
															HorizontalAlignment="Stretch" Width="{TemplateBinding Width}" x:Name="default" BorderThickness="1"
                                                                         Margin="0" Padding="2,0,2,0" FontFamily="Verdana" FontSize="9" AcceptsReturn="True"
															IsEnabled="{Binding CanWrite}" Grid.Column="1" >
                                                        </TextBox>
                                                    </Grid>
                                                </DataTemplate>
                                                <DataTemplate x:Key="Type:Object">
                                                    <!--<controls:Expander IsExpanded="False">
                                                        <controls:HeaderedContentControl.Header>
                                                            <TextBlock Text="{Binding Name}" />
                                                        </controls:HeaderedContentControl.Header>
                                                        <controls:HeaderedContentControl.Content>-->
                                                            <sl:PropertyGrid x:Name="t" Instance="{Binding Value}" />
                                                        <!--</controls:HeaderedContentControl.Content>
                                                    </controls:Expander>-->
                                                </DataTemplate>
                                                <DataTemplate x:Key="Type:DateTime">
                                                    <Grid >
                                                        <Grid.ColumnDefinitions>
                                                            <ColumnDefinition Width="40*" />
                                                            <ColumnDefinition Width="60*" />
                                                        </Grid.ColumnDefinitions>
                                                        <TextBlock  Margin="0,0,1,0" TextAlignment="Right" 
                                                                            Padding="0" FontFamily="Verdana" FontSize="9"
											 Text="{Binding Name}" VerticalAlignment="Top" Grid.Column="0" />

                                                        <basics:DatePicker SelectedDate="{Binding Value, Mode=TwoWay}" 
															 Grid.Column="1"	IsEnabled="{Binding CanWrite}" ></basics:DatePicker>

                                                    </Grid>
                                                </DataTemplate>


                                                <DataTemplate x:Key="Type:Boolean">
                                                    <Grid >
                                                        <Grid.ColumnDefinitions>
                                                            <ColumnDefinition Width="40*" />
                                                            <ColumnDefinition Width="60*" />
                                                        </Grid.ColumnDefinitions>
                                                        <TextBlock  Margin="0,0,1,0" TextAlignment="Right" 
                                                                            Padding="0" FontFamily="Verdana" FontSize="9"
											 Text="{Binding Name}" VerticalAlignment="Top" Grid.Column="0" />

                                                        <CheckBox IsChecked="{Binding Path=Value, Mode=TwoWay}" IsEnabled="{Binding CanWrite}"
																Grid.Column="1" Padding="0"
																/>

                                                    </Grid>
                                                </DataTemplate>
                                                <DataTemplate x:Key="Type:Enum">
                                                    <Grid >
                                                        <Grid.ColumnDefinitions>
                                                            <ColumnDefinition Width="40*" />
                                                            <ColumnDefinition Width="60*" />
                                                        </Grid.ColumnDefinitions>
                                                        <TextBlock  Margin="0,0,1,0" TextAlignment="Right" 
                                                                            Padding="0" FontFamily="Verdana" FontSize="9"
											 Text="{Binding Name}" VerticalAlignment="Top" Grid.Column="0" />

                                                        <ComboBox  
									ItemsSource="{Binding Path=Value, Converter={StaticResource enumTypeConverter}}" 
									SelectedItem="{Binding Path=Value, Mode=TwoWay, Converter={StaticResource enumValueConverter}}" 
                                                                    Margin="0" Padding="2,0,2,0" FontFamily="Verdana" FontSize="9"
									DisplayMemberPath="Name" IsEnabled="{Binding CanWrite}"  Grid.Column="1" />

                                                    </Grid>

                                                </DataTemplate>
                                            </ItemsControl.Resources>
                                            <ItemsControl.ItemTemplate>
                                                <DataTemplate>
                                                    <Border CornerRadius="5" Margin="0" Padding="0" >
                                                        <sl:ContentControlWithTemplateSelector Content="{Binding}" 
											ContentTemplateSelector="{StaticResource  propertiesTemplateSelector}"
											 Grid.Column="2" HorizontalContentAlignment="Stretch"  
											 />

                                                    </Border>
                                                </DataTemplate>
                                            </ItemsControl.ItemTemplate>

                                        </ItemsControl>



                                        <!--</controls:HeaderedContentControl.Content>
                                        </controls:Expander>-->

                                    </DataTemplate>
                                </ItemsControl.ItemTemplate>
                            </ItemsControl>
                        </StackPanel>
                    </ScrollViewer>
                </ControlTemplate>

            </Setter.Value>
        </Setter>
    </Style>

</ResourceDictionary>